import pickle
import cv2
import time
import numpy as np
from socket import socket,SOL_SOCKET,SO_REUSEADDR
import struct

HOST = '0.0.0.0'
PORT = 7777
ADDR = (HOST,PORT)
sockfd = socket()
sockfd.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
sockfd.bind(ADDR)
sockfd.listen(5)
print("Listen to the port 7777...")
connfd,addr = sockfd.accept()
print('conect from', addr)

with open('./app/static/pikle_data/size_list.pikle','rb') as f:
    pk_data = f.read()

lst = pickle.loads(pk_data)

with open('./app/static/pikle_data/datas.pikle','rb') as datas:
    for size in lst:
        data = datas.read(size)
        pakage = struct.pack('i',size)
        connfd.send(pakage)
        connfd.send(data)
        time.sleep(0.05)


# 播放,用于测试保存的数据
'''
with open('./app/static/pikle_data/size_list.pikle','rb') as f:
    pk_data = f.read()

lst = pickle.loads(pk_data)
print(lst)
with open('./app/static/pikle_data/datas.pikle','rb') as datas:
    for size in lst:
        data = datas.read(size)
        data = pickle.loads(data)
        print('size:',size)
        print(type(data['img']))
        cv2.imshow("frame",data['img'])
        time.sleep(0.04)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break 
'''


